﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using FastUtil.Base;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("Attach")]
    public class AttachController : BaseController
    {
        [AutoWired]
        public DB_Attach @DB_Attach;
        public string TemplatePath = System.Configuration.ConfigurationManager.AppSettings["AttachTempPath"];
        [HttpPost, Route("Upload")]
        public ActionResult Upload()
        {
            ReturnValue result = new ReturnValue();
            HttpFileCollectionBase FileList = Request.Files;
            String GroupGuid = Request.Form["GroupGuid"];
            String GroupType = Request.Form["GroupType"];
            List<string> AttachGuidList = new List<string>();
            for (int i = 0; i < FileList.Count; i++)
            {
                HttpPostedFileBase File = FileList[i];
                Frame_AttachInfo AttachInfo = new Frame_AttachInfo();
                AttachInfo.FileName = File.FileName;
                AttachInfo.ContentType = File.ContentType;
                AttachInfo.FileSize = File.ContentLength;
                AttachInfo.GroupGuid = GroupGuid;
                AttachInfo.GroupType = GroupType;
                byte[] FileContent = ConvertUtil.ConvertStream2Byte(File.InputStream);
                @DB_Attach.Insert(AttachInfo, FileContent);
                AttachGuidList.Add(AttachInfo.RowGuid);
            }
            result.Add("attachguidlist", AttachGuidList);
            return result;
        }

        [HttpPost, Route("Delete")]
        public ActionResult Delete()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string RowGuid = Convert.ToString(Params.GetValue("RowGuid"));
            @DB_Attach.DeleteAttach(RowGuid);
            return result;
        }

        [HttpPost, Route("DeleteGroup")]
        public ActionResult DeleteGroup()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string GroupGuid = Convert.ToString(Params.GetValue("GroupGuid"));
            string GroupType = Convert.ToString(Params.GetValue("GroupType"));
            @DB_Attach.DeleteGroupAttach(GroupGuid, GroupType);
            return result;
        }



        [HttpPost, Route("List")]
        public ActionResult List()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            String GroupGuid = Convert.ToString(Params.GetValue("GroupGuid"));
            String GroupType = Convert.ToString(Params.GetValue("GroupType"));
            List<Frame_AttachInfo> AttachList = @DB_Attach.GetAttachList(GroupGuid, GroupType);
            result.Add("FileList", AttachList);
            return result;
        }

        [HttpGet, Route("ReadFile")]
        public ActionResult ReadFile()
        {
            ReturnValue result = new ReturnValue();
            String RowGuid = Request.QueryString["RowGuid"];
            Frame_AttachInfo AttachInfo = @DBUtil.FindOne<Frame_AttachInfo>(RowGuid);
            if (AttachInfo.StorageType == "Local")
            {
                Response.Redirect(CommonUtil.GetRootPath() + AttachInfo.VirtualPath);
            }
            else if (AttachInfo.StorageType == "DB")
            {
                //暂存本地
                Frame_AttachStorage AttachStorage = @DBUtil.FindOne<Frame_AttachStorage>(AttachInfo.StorageGuid, AttachInfo.ConfigName);
                string UploadVirtualPath = System.Web.HttpContext.Current.Server.MapPath("~/" + TemplatePath);
                DirectoryInfo UploadPath = new DirectoryInfo(UploadVirtualPath);
                if (!UploadPath.Exists)
                {
                    UploadPath.Create();
                }
                string UploadFileFloderPath = TemplatePath + "/" + AttachInfo.RowGuid;
                DirectoryInfo UploadFileFloder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/" + UploadFileFloderPath));
                if (!UploadFileFloder.Exists)
                {
                    UploadFileFloder.Create();
                }
                string UploadFileVirtualPath = UploadFileFloderPath + "/" + AttachInfo.FileName;
                if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/" + UploadFileVirtualPath)))
                {
                    FileUtil.SaveFile(System.Web.HttpContext.Current.Server.MapPath("~/" + UploadFileVirtualPath), AttachStorage.Content);
                }
                Response.Redirect(CommonUtil.GetRootPath() + UploadFileVirtualPath);
            }
            return result;
        }

    }
}
